package com.hy.study.mapper;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 继承mapper 这里需要写4个参数 参数一：输入数据的key 表示文件行的偏移量 参数二：输入数据的value 表示文件一行的数据 这里是Text 字符串类型 参数三：输出数据的key Text 参数四: 输出数据的value IntWritable
 *
 * @author huyong
 * @date 2024 年 01 月 29 日 11:16: 31
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final Text text = new Text();
    private final IntWritable intWritable = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        // 获取一行
        String line = value.toString();
        // 切分数据
        String[] words = line.split(" ");

        // 循环写出
        for (String word : words) {
            text.set(word);
            context.write(text, intWritable);
        }
    }
}
